﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Controls
{
    public partial class gridViewColumnNew : Panel
    {
        public gridViewColumnNew()
        {
            InitializeComponent();
        }
        public gridViewColumnNew(String columnNames)
        {
            InitializeComponent();
            CreateControls(columnNames);
        }

        public delegate void DeleteButtonClick(object sender);

        public event DeleteButtonClick DeleteBtnClicked;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

    

        private void CreateControls(string columnNames)
        {
            this.AutoSize = true;
            Graphics graph = CreateGraphics();
            if (!string.IsNullOrEmpty(columnNames))
            {
                ComboBox combox1 = new ComboBox();
                LinkLabel linkLabel1 = new LinkLabel();
                TextBox tb = new TextBox();
                string[] strs = columnNames.Split(new Char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

                //等于不等于，包含不包含
                combox1.FormattingEnabled = true;
                combox1.Items.AddRange(new object[] { "包含", "不包含" });
                combox1.Name = "combox1";
                //计算字符串的像素尺寸
                SizeF sizeF = graph.MeasureString("不包含", this.Font);
                combox1.Size = new Size((int)(Math.Ceiling(sizeF.Width)) + 20, (int)(Math.Ceiling(sizeF.Height)));
                combox1.Left = 3;
                combox1.Top = this.Location.Y;
                combox1.DropDownStyle = ComboBoxStyle.DropDownList;
                combox1.SelectedIndex = strs[0].ToString().Trim()=="包含"?0:1;
                this.Controls.Add(combox1);

                //输入文本框
                tb.Width = 250;
                tb.Height = 16;
                tb.Left = combox1.Right + 5;
                tb.Top = this.Location.Y;
                tb.Text = strs[1].ToString();
                this.Controls.Add(tb);

                //删除
                linkLabel1.Name = "linkLabel1";
                linkLabel1.Text = "删除";
                SizeF sf = graph.MeasureString(linkLabel1.Text, this.Font);
                linkLabel1.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
                linkLabel1.Height = (int)(Math.Ceiling(sizeF.Height));
                linkLabel1.Left = tb.Right + 5;
                linkLabel1.Top = this.Location.Y;
                linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
                this.Controls.Add(linkLabel1);
            }
            else
            {
                ComboBox combox1 = new ComboBox();
                LinkLabel linkLabel1 = new LinkLabel();
                TextBox tb = new TextBox();
                //等于不等于，包含不包含
                combox1.FormattingEnabled = true;
                combox1.Items.AddRange(new object[] { "包含", "不包含" });
                combox1.Name = "combox1";
                //计算字符串的像素尺寸
                SizeF sizeF = graph.MeasureString("不包含", this.Font);
                combox1.Size = new Size((int)(Math.Ceiling(sizeF.Width)) + 20, (int)(Math.Ceiling(sizeF.Height)));
                combox1.Left = 3;
                combox1.Top = this.Location.Y;
                combox1.DropDownStyle = ComboBoxStyle.DropDownList;
                this.Controls.Add(combox1);

                //输入文本框
                tb.Width = 250;
                tb.Height = 16;
                tb.Left = combox1.Right + 5;
                tb.Top = this.Location.Y;
                this.Controls.Add(tb);

                //删除
                linkLabel1.Name = "linkLabel1";
                linkLabel1.Text = "删除";
                SizeF sf = graph.MeasureString(linkLabel1.Text, this.Font);
                linkLabel1.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
                linkLabel1.Height = (int)(Math.Ceiling(sizeF.Height));
                linkLabel1.Left = tb.Right + 5;
                linkLabel1.Top = this.Location.Y;
                linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
                this.Controls.Add(linkLabel1);
            }
        }

        public string GetCondition()
        {
            StringBuilder st = new StringBuilder();
            foreach (var obj in this.Controls)
            {
                if (obj is ComboBox)
                {
                    if (string.IsNullOrEmpty(((ComboBox)obj).Text))
                    {
                        MessageBox.Show("请添加完整信息");
                        return "请添加完整信息";
                    }
                    //if (((ComboBox)obj).Text == "包含")
                    //{
                    //    st.Append(" like ");

                    //}
                    //else if (((ComboBox)obj).Text == "不包含")
                    //{
                    //    st.Append(" not like ");
                    //}
                    st.Append(((ComboBox)obj).Text);
                }
                if (obj is TextBox)
                {
                    if (string.IsNullOrEmpty(((TextBox)obj).Text))
                    {
                        MessageBox.Show("请添加完整信息");
                        return "请添加完整信息";
                    }
                    st.Append("-" + ((TextBox)obj).Text + ",");
                }
            }
            return st.ToString();
        }

        private void colLinkButton_Click(object sender, EventArgs e)
        {
            DeleteBtnClicked(this);
        }
    }
}
